package com.example.fw;

import java.util.Random;

import org.openqa.selenium.By;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		
//		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
//		Integer box = checkboxes.size();
		
		manager.navigateTO().mainPage();
		Integer box = driver.findElements(By.name("selected[]")).size();
		for (int i = 0; i < box; i++) {
			String firstname = driver.findElement(By.xpath("//table/tbody/tr[" + (i + 2) + "]/td[3]")).getText();
			String lastname = driver.findElement(By.xpath("//table/tbody/tr[" + (i + 2) + "]/td[2]")).getText();
			cachedContacts.add(new ContactData()
										.withFirstname(firstname)
										.withLastname(lastname));
		}
	}
	
	public ContactHelper createContact(ContactData contact, boolean formType) {
		manager.navigateTO().mainPage();
		gotoEditPage();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact, boolean formType) {
		manager.navigateTO().mainPage();
		gotoEditContactPage(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
		return this;
	}


	public ContactHelper deleteContact(int index) {
		manager.navigateTO().mainPage();
		gotoEditContactPage(index);
		deleteContact();
		returnToHomePage();
		rebuildCache();
		return this;
	}

// ---------------------------------------------------------------------
	
	public ContactHelper gotoEditPage() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomephone());
		type(By.name("mobile"), contact.getMobilephone());
		type(By.name("work"), contact.getWorkphone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), getNameMonth(contact.getBmonth()));
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION) {
			Random rnd= new Random();
			Integer realQuantity = getQuantityGroups();
			selectByText(By.name("new_group"), getNameGroup("" + (rnd.nextInt(realQuantity) + 1)));
		} 
//		else {
//			if (driver.findElements(By.name("new_group")).size() != 0) {
//				throw new Error("Group selector exists in contact modification form");
//			}
//		}
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getHome2());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper gotoEditContactPage(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index + 1) + "]"));
		return this;
		
	}

	public ContactHelper deleteContact() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		cachedContacts = null;
		return this;
	}

	public String getNameMonth(String index) {
		if (index != null) {
			return driver.findElement(By.xpath("//select[2]/option[" + index + "]")).getText();
		}
		return index;
	}
	
	public String getNameGroup(String index) {
		if (index != null) {
			return driver.findElement(By.xpath("//select[3]/option[" + index + "]")).getText();
		}
		return index;
	}
	
	public int getQuantityGroups() {
		return driver.findElements(By.xpath("//select[3]/option")).size();
	}

}
